#include "coursecard.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QDateTime>

CourseCard::CourseCard(QWidget *parent) : QWidget(parent) {
    initUI();
}

void CourseCard::initUI() {
    // 创建控件
    m_pCourseName = new QLabel(tr("课程名称"), this);
    m_pCourseTime = new QLabel(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"), this);
    m_pCourseImage = new QLabel(this);
    m_pCourseStatus = new QLabel(tr("状态"), this);
    m_pEnterButton = new QPushButton(tr("进入教室"), this);
    
    // 进入教室按钮设置样式  113, 153, 243    113, 153, 243   77, 128, 240
    m_pEnterButton->setStyleSheet("QPushButton { background-color: #4d80f0; color: white; border: none; border-radius: 4px; padding: 5px 10px; }"
                                 "QPushButton:hover { background-color: #7199f3; }"
                                 "QPushButton:pressed { background-color: #337ecc; }");

    // 设置课程图片标签
    m_pCourseImage->setFixedSize(64, 64); // 设置固定大小
    m_pCourseImage->setStyleSheet("border: 1px solid gray; border-radius: 4px;");
    m_pCourseImage->setAlignment(Qt::AlignCenter);
    m_pCourseImage->setText(tr("课程图片"));
    
    // 设置样式
    m_pCourseName->setStyleSheet("font-weight: bold;");
    m_pCourseStatus->setStyleSheet("color: green; font-size: 12px;");
    
    // 创建布局
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    
    // 第一行：图片和课程名称
    QHBoxLayout* firstRowLayout = new QHBoxLayout();
    firstRowLayout->addWidget(m_pCourseImage);
    firstRowLayout->addWidget(m_pCourseName);
    firstRowLayout->addStretch(); // 添加弹性空间
    mainLayout->addLayout(firstRowLayout);
    
    // 第二行：状态显示
    mainLayout->addWidget(m_pCourseStatus);
    
    // 第三行：课程时间和进入教室按钮，两端对齐
    QHBoxLayout* thirdRowLayout = new QHBoxLayout();
    thirdRowLayout->addWidget(m_pCourseTime);
    thirdRowLayout->addStretch(); // 添加弹性空间
    thirdRowLayout->addWidget(m_pEnterButton);
    mainLayout->addLayout(thirdRowLayout);
    
    // 设置整体边距和间距
    mainLayout->setSpacing(5);
    mainLayout->setContentsMargins(10, 10, 10, 10);
    
    setLayout(mainLayout);
    // 为整个卡片设置圆角边框样式
    setStyleSheet("CourseCard { border: 1px solid #ccc; border-radius: 8px; background-color: #f9f9f9; }");
}

void CourseCard::setCourseInfo(const QString& name, const QString& time, const QString& status)
{
    m_pCourseName->setText(name);
    m_pCourseTime->setText(time);
    m_pCourseStatus->setText(status);
}